<template>
  <div>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="公告标题">
        <el-input
          clearable
          size="small"
          v-model="formInline.title"
          auto-complete="off"
          placeholder="公告标题"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="getData">查询</el-button>
        <el-button size="small" type="primary" @click="openDiaLog()" v-show="userInfo.role == 1">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      size="small"
      :data="datalist"
      highlight-current-row
      border
      element-loading-text="拼命加载中"
      style="width: 100%;"
    >
      <el-table-column align="center" sortable prop="title" label="标题" min-width="200"></el-table-column>
      <el-table-column align="center" sortable prop="detail" label="详情" min-width="200"></el-table-column>
      <el-table-column align="center" sortable prop="createTime" label="发布时间" width="250"></el-table-column>
      <el-table-column align="center" label="操作" width="150" >
        <template slot-scope="scope">
          <el-button v-show="userInfo.role == 1" size="mini" type="danger" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="page-box"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      background
      :current-page="pageparm.currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageparm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageparm.total"
    ></el-pagination>
    <el-dialog title="发布游戏公告" :visible.sync="dialogVisible" width="50%" @click="closeDialog()">
      <el-form label-width="100px" :model="addData">
        <el-form-item label="更新标题" prop="title" style="width:60%">
          <el-input size="small" v-model="addData.title" auto-complete="off" placeholder="请输入更新标题"></el-input>
        </el-form-item>
        <el-form-item label="更新内容" prop="detail">
          <el-input
            size="small"
            v-model="addData.detail"
            type="textarea"
            auto-complete="off"
            placeholder="请输入职位描述"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog()">取消</el-button>
        <el-button size="small" type="primary" class="title" @click="add()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getList, insert, del } from "../../api/youxi";
export default {
  data() {
    return {
      formInline: {},
      datalist: [],
      pageparm: {
        currentPage: 0,
        pageSize: 10,
        total: 0
      },
      dialogVisible: false,
      addData: {},
      userInfo:{}
    };
  },
  created() {
    this.userInfo = JSON.parse(sessionStorage.getItem("sysuser"));
    this.getData();
  },
  methods: {
    del(row) {
      let param = {};
      param.id = row.id;
      del(param).then(res => {
        if (res.status == 200) {
          this.$message({
            type: "success",
            message: "删除成功"
          });
          this.getData();
        } else {
            this.$message({
            type: "error",
            message: "删除失败"
          });
        }
      });
    },
    add() {
      insert(this.addData).then(res => {
        if (res.status == 200) {
          this.$message({
            type: "success",
            message: "发布成功"
          });
          this.dialogVisible = false;
          this.getData();
        } else {
          this.$message({
            type: "error",
            message: "发布失败"
          });
        }
      });
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    openDiaLog() {
      this.addData = {};
      this.dialogVisible = true;
    },
    getData() {
      this.formInline.pageIndex = this.pageparm.currentPage;
      this.formInline.pageSize = this.pageparm.pageSize;
      getList(this.formInline).then(res => {
        if (res.status == 200) {
          this.datalist = res.data;
          this.pageparm.total = res.total;
        }
      });
    },
    handleCurrentChange(val) {
      this.pageparm.currentPage = val;
      this.getData();
    },
    handleSizeChange(val) {
      this.pageparm.pageSize = val;
      this.getData();
    }
  }
};
</script>